package com.blb.controller;

import com.blb.dto.ZType;
import com.blb.dto.ZTypeExample;
import com.blb.service.ZTypeService;
import com.blb.utils.LayUIDataTable;
import com.blb.utils.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/type")
@CrossOrigin
public class TypeController {
    @Autowired
    private ZTypeService zTypeService;


    @RequestMapping("/querymain")
    @ResponseBody
    public LayUIDataTable querypidZero(Model model)      //查询顶级分类
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        ZTypeExample.Criteria criteria1 = criteria.andPidEqualTo(0);
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        dt.setCode(Status.SUCCESS);
        dt.setCount(zTypes.size());
        dt.setMsg("查询成功!");
        dt.setData(zTypes);
        return dt;
    }

    @RequestMapping("/querynotmain")
    @ResponseBody
    public LayUIDataTable querypidnotZero(Model model)  //查询非顶级分类
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        zTypeExample.setOrderByClause("trenqi DESC limit 0,6");
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        ZTypeExample.Criteria criteria1 = criteria.andPidNotEqualTo(0);
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        dt.setCode(Status.SUCCESS);
        dt.setCount(zTypes.size());
        dt.setMsg("查询成功!");
        dt.setData(zTypes);
        return dt;
    }

    @RequestMapping("/queryid/{tid}")
    @ResponseBody
    public LayUIDataTable queryByid(Model model,@PathVariable("tid") int tid)  //按照tid查询类型
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZType zType = zTypeService.selectByPrimaryKey(tid);
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("查询成功!");
        dt.setData(zType);
        return dt;
    }

    @RequestMapping("/querypid/{pid}")
    @ResponseBody
    public LayUIDataTable queryBypid(Model model,@PathVariable("pid") int pid)  //按照pid查询类型
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("查询成功!");
        dt.setData(zTypes);
        return dt;
    }

    @RequestMapping("/queryname/{tname}")
    @ResponseBody
    public LayUIDataTable queryByname(Model model,@PathVariable("tname") String tname)  //按照tid查询类型
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        criteria.andTnameLike(tname);
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("查询成功!");
        dt.setData(zTypes);
        return dt;
    }


    @RequestMapping("/querysplit")
    @ResponseBody
    public LayUIDataTable querysplit(Model model)
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZTypeExample zTypeExample = new ZTypeExample();
        ZTypeExample.Criteria criteria = zTypeExample.createCriteria();
        ZTypeExample.Criteria criteria1 = criteria.andPidEqualTo(0);
        List<ZType> zTypes = zTypeService.selectByExample(zTypeExample);
        List<Object> objects = new ArrayList<>();
        for (ZType zType:zTypes) {

            ZTypeExample zTypeExample2 = new ZTypeExample();
            ZTypeExample.Criteria criteria2= zTypeExample2.createCriteria();
            criteria2.andPidEqualTo(zType.getTid());
            List<ZType> zTypes1 = zTypeService.selectByExample(zTypeExample2);
            objects.add(zTypes1);
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(objects.size());
        dt.setMsg("查询成功!");
        dt.setData(objects);
        return dt;
    }
}
